package com.css.dzj.dzjap.modules.publick.service.impl;

import com.css.dzj.dzjap.common.util.Constant;
import com.css.dzj.dzjap.common.util.R;
import com.css.dzj.dzjap.common.util.ServiceUtil;
import com.css.dzj.dzjap.modules.enterprise.entity.ApEnterpriseCreditEntity;
import com.css.dzj.dzjap.modules.enterprise.service.EnterpriseJSService;
import com.css.dzj.dzjap.modules.enterprise.service.EnterpriseService;
import com.css.dzj.dzjap.modules.expertcredit.entity.ApExpertCreditEntity;
import com.css.dzj.dzjap.modules.expertcredit.service.ExpertCreditService;
import com.css.dzj.dzjap.modules.lawenforcesuper.entity.ApDeptExtractInfoEntity;
import com.css.dzj.dzjap.modules.lawenforcesuper.entity.ApSupersiveTaskInfoEntity;
import com.css.dzj.dzjap.modules.lawenforcesuper.entity.QApDeptExtractInfoEntity;
import com.css.dzj.dzjap.modules.lawenforcesuper.entity.QApSupersiveTaskInfoEntity;
import com.css.dzj.dzjap.modules.lawenforcesuper.service.AllSuperviseService;
import com.css.dzj.dzjap.modules.lawenforcesuper.service.ApDeptExtractService;
import com.css.dzj.dzjap.modules.lawenforcesuper.service.SuperTaskService;
import com.css.dzj.dzjap.modules.project.generecord.entity.Generecord;
import com.css.dzj.dzjap.modules.project.generecord.service.GenerecordService;
import com.css.dzj.dzjap.modules.project.supervision.entity.Supervision;
import com.css.dzj.dzjap.modules.project.supervision.service.SupervisionService;
import com.css.dzj.dzjap.modules.publick.dao.PublicDao;
import com.css.dzj.dzjap.modules.publick.entity.ApPublicEntity;
import com.css.dzj.dzjap.modules.publick.entity.QApPublicEntity;
import com.css.dzj.dzjap.modules.publick.service.PublicService;
import com.css.dzj.dzjap.modules.record.approject.entity.ApRecordEntity;
import com.css.dzj.dzjap.modules.record.approject.service.ApRecordService;
import com.css.dzj.dzjap.modules.record.community.entity.ApRecordGainEntity;
import com.css.dzj.dzjap.modules.record.community.service.CommunityRecordService;
import com.css.dzj.dzjap.modules.todolist.entity.ApTodoEntity;
import com.css.dzj.dzjap.modules.todolist.service.ApTodoService;
import com.css.dzj.dzjap.sys.user.entity.LoginUser;
import com.querydsl.core.QueryResults;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class PublicServiceImpl implements PublicService {

    @Autowired
    private PublicDao publicDao;

    //注入JPAQueryFactory工厂
    @Autowired
    JPAQueryFactory jpaQueryFactory;

    @Autowired
    HttpServletRequest request;

    @Autowired
    ServiceUtil serviceUtil;

    //专家信用信息表
    @Autowired
    private ExpertCreditService expertCreditService;

    //评价单位信用信息表
    @Autowired
    private EnterpriseService enterpriseService;

    //建设单位信用信用表
    @Autowired
    private EnterpriseJSService enterpriseJsService;

    @Autowired
    private ApRecordService apRecordService;

    //一般工程备案表
    @Autowired
    private GenerecordService generecordService;

    //监管工作
    @Autowired
    private SupervisionService supervisionService;

    //小区化
    @Autowired
    private CommunityRecordService communityRecordService;

    //安评单位抽取信息
    @Autowired
    private ApDeptExtractService apDeptExtractService;

    //项目抽取抽取
    @Autowired
    private SuperTaskService superTaskService;

    //全覆盖抽取
    @Autowired
    private AllSuperviseService allSuperviseService;
    @Autowired
    private ApTodoService apTodoService;



    /**
     * 通过主键Id查询公开公告表
     */
    @Override
    public ApPublicEntity getPublick(String publicId) {
        if(StringUtils.isEmpty(publicId) ){

            throw new RuntimeException("参数为空");
        }
        QApPublicEntity qPub = QApPublicEntity.apPublicEntity;
        return jpaQueryFactory
                .selectFrom(qPub)
                .where( qPub.publicId.eq(publicId),
                        qPub.delFlag.eq(Constant.NO_DELETE)
                )
                .fetchOne();
    }

    /**
     * 添加公开公告表
     */
    @Override
    @Transactional
    public String addPublick(ApPublicEntity pub) {

          R r = serviceUtil.getUserInfoByRequest(request);
          if(r.getCode().equals("200")){
              LoginUser userInfo =  (LoginUser)r.get("data");
            pub.setCreateUser(userInfo.getLoginName());
            pub.setOrganName(userInfo.getOrgName());
          }

        pub.setRevokeg(Constant.REVOKE_NO);
        pub.setDelFlag(Constant.NO_DELETE);
        pub.setInputenTime(new Date());
        pub.setCreateTime(new Date());
        pub.setFictionType("0");
        try {
            publicDao.save(pub);
            //调取待办接口
            ApTodoEntity apTodoEntity = new ApTodoEntity();
            apTodoEntity.setStatus(Constant.STATUSDB);
            apTodoEntity.setCreateTime(new Date());
            apTodoEntity.setDelFlag(Constant.NO_DELETE);
            apTodoEntity.setTitle(pub.getTitle()+"公开公告审批");
            apTodoEntity.setLinkId(pub.getPublicId());
            //审批员才能看到相应的审批
            apTodoEntity.setStation("地震部门审定人员");
            //去审批页
            apTodoEntity.setUrl("../todolist/publick.html");
            apTodoEntity.setLocation(pub.getOrganName());
            apTodoEntity.setCreatePerson(ServiceUtil.getLoginName(request));
            //审批的业务
            apTodoEntity.setTypeFlag(Constant.TYPRFLAGSS);
            apTodoService.saveDB(apTodoEntity);
            return pub.getPublicId();

        }catch (Exception e){
            return "0";
        }
    }

    /**
     * 修改公开公告表
     */
    @Override
    @Transactional
    public void update(ApPublicEntity pub) {


    }
    /**
     * 修改公开公告表
     */
    @Override
    @Transactional
    public void updatePublic(ApPublicEntity pub) {
       String pubtype=pub.getPublishType();
       if("0".equals(pubtype)){
           //调取待办接口
           ApTodoEntity apTodoEntity = new ApTodoEntity();
           apTodoEntity.setStatus(Constant.STATUSDB);
           apTodoEntity.setCreateTime(new Date());
           apTodoEntity.setDelFlag(Constant.NO_DELETE);
           apTodoEntity.setTitle(pub.getTitle()+"公开公告审批");
           apTodoEntity.setLinkId(pub.getPublicId());
           //审批员才能看到相应的审批
           apTodoEntity.setStation("地震部门审定人员");
           //去审批页
           apTodoEntity.setUrl("../todolist/publick.html");
           apTodoEntity.setLocation(pub.getOrganName());
           apTodoEntity.setCreatePerson(ServiceUtil.getLoginName(request));
           //审批的业务
           apTodoEntity.setTypeFlag(Constant.TYPRFLAGSS);
           apTodoService.saveDB(apTodoEntity);
           //修改审批状态为待审核
           pubtype=null;
           pub.setPublishType(pubtype);
       }
       update(pub);
    }

    /**
     * 审批公开公告表
     */
    @Override
    @Transactional
    public void approvalPublick(ApPublicEntity pub) {
        update(pub);
        //查询待办里有没有此业务审批
        ApTodoEntity apTodoEntity=apTodoService.findByIdandType(pub.getPublicId(), Constant.TYPRFLAGSS);
        if(apTodoEntity!=null){
            apTodoEntity.setStatus(Constant.STATUSYB);
            apTodoService.update(apTodoEntity);
        }

    }
    /**
     * 删除公开公告表
     */
    @Override
    @Transactional
    public Boolean deletePublick(String publicId) {

        try{
            ApPublicEntity pub = getPublick(publicId);

            if(pub.getFictionType().equals("1")){

                //删除专家公告信息
                ApExpertCreditEntity expertCred = expertCreditService.getExpertCred(pub.getRelationId());
                if(expertCred!=null){
                    expertCred.setExpertNotice("0");
                    expertCreditService.update(expertCred);
                }
                //删除专家黑名单公告信息
                ApExpertCreditEntity expertCred1 = expertCreditService.getReExpertCred(pub.getRelationId());
                if(expertCred1!=null){
                    expertCred1.setExpertNotice("0");
                    expertCreditService.update(expertCred1);
                }

            } else if(pub.getFictionType().equals("2")){
                //删除评价或建设单位公告信息
                ApEnterpriseCreditEntity enterPrise = enterpriseService.getEnterPrise(pub.getRelationId(),pub.getEnterType());
                if(enterPrise!=null){
                    enterPrise.setEnterpriseNotice("0");
                    enterpriseService.update(enterPrise);
                }
                //删除黑名单评价或建设单位公告信息
                ApEnterpriseCreditEntity enterPrise1 = enterpriseService.getReEnterPrise(pub.getRelationId(),pub.getEnterType());
                if(enterPrise1!=null){
                    enterPrise1.setEnterpriseNotice("0");
                    enterpriseService.update(enterPrise1);
                }

            } else if(pub.getFictionType().equals("5")){
                ApRecordEntity apRecordEntity = apRecordService.queryApRecordById(pub.getRelationId());
                if(apRecordEntity!=null){
                    apRecordEntity.setRecordPublicStauts(Constant.PUBLICNO);
                    apRecordService.update(apRecordEntity);
                }

            } else if(pub.getFictionType().equals("3")){
                Generecord generecord = generecordService.queryById(pub.getRelationId());
                if(generecord!=null){
                    generecord.setNoticeStatus("0");
                    generecordService.update(generecord);
                }
            } else if(pub.getFictionType().equals("4")){
                Supervision supervision = supervisionService.queryById(pub.getRelationId());
                if(supervision!=null){
                    supervision.setNoticeStatus("0");
                    supervisionService.update(supervision);
                }

            } else if(pub.getFictionType().equals("6")){
                ApRecordGainEntity apRecordGainEntity = communityRecordService.queryApRecordGainById(pub.getRelationId());
                if(apRecordGainEntity!=null){
                    apRecordGainEntity.setRecordPublicStauts(Constant.PUBLICNO);
                    communityRecordService.update(apRecordGainEntity);
                }
            } else if(pub.getFictionType().equals("7")){
                ApDeptExtractInfoEntity apDeptExtractInfoEntity = apDeptExtractService.queryExtractInfoById(pub.getRelationId());
                if(apDeptExtractInfoEntity!=null){
                    QApDeptExtractInfoEntity qDept = QApDeptExtractInfoEntity.apDeptExtractInfoEntity;

                    jpaQueryFactory
                            .update(qDept)
                            .set(qDept.enterpriseNotice,"0")
                            .where(
                                    qDept.id.eq(pub.getRelationId())
                            )
                            .execute();
                }
            } else if(pub.getFictionType().equals("8")){
                ApSupersiveTaskInfoEntity apSupersiveTaskInfoEntity = superTaskService.queryApSupersiveTaskInfoById(pub.getRelationId());
                if(apSupersiveTaskInfoEntity!=null){
                    QApSupersiveTaskInfoEntity qSuper = QApSupersiveTaskInfoEntity.apSupersiveTaskInfoEntity;

                    jpaQueryFactory
                            .update(qSuper)
                            .set(qSuper.enterpriseNotice,"0")
                            .where(
                                    qSuper.id.eq(pub.getRelationId())
                            )
                            .execute();
                }

            } else if(pub.getFictionType().equals("10")){
                ApRecordEntity apRecordEntity = apRecordService.queryApRecordById(pub.getRelationId());
                if(apRecordEntity!=null){
                    apRecordEntity.setExtractPublicStatus("0");
                    apRecordService.update(apRecordEntity);
                }

            }

        QApPublicEntity qPub = QApPublicEntity.apPublicEntity;
        if(StringUtils.isEmpty(publicId)){
            return  false;
        }

            jpaQueryFactory
                    .update(qPub)
                    .set(qPub.delFlag, Constant.DELETE)
                    .where(
                            qPub.publicId.eq(publicId),
                            qPub.delFlag.eq(Constant.NO_DELETE)

                    )
                    .execute();
            return true;
        }catch (Exception e){
            return false;
        }
    }

    /**
     * 分页查询所有公开公告表
     */
    @Override
    public QueryResults<ApPublicEntity> getAllPublick(Pageable pageable) {
        QApPublicEntity qPub = QApPublicEntity.apPublicEntity;
        Predicate predicate = qPub.isNotNull().or(qPub.isNull());
        List<String> stationList=new ArrayList<String>();
        R r = serviceUtil.getUserInfoByRequest(request);
        if(r.getCode().equals("200")){
            LoginUser userInfo =  (LoginUser)r.get("data");
            stationList=userInfo.getRole();

            if(stationList!=null && !stationList.contains("地震部门审定人员")){
                predicate = ExpressionUtils.and(predicate, qPub.createUser.eq(userInfo.getLoginName()));
                predicate = ExpressionUtils.and(predicate, qPub.organName.eq(userInfo.getOrgName()));
            }
        }
        predicate = ExpressionUtils.and(predicate, qPub.delFlag.eq(Constant.NO_DELETE)) ;

        return  jpaQueryFactory
                .selectFrom(qPub)
                .where(
                        predicate
                )
                .orderBy(qPub.createTime.desc())
                .offset(pageable.getOffset())   //起始页
                .limit(pageable.getPageSize())  //每页大小
                .fetchResults();
    }



    /**
     * 多条件分页查询所有公开公告表
     */
    @Override
    public QueryResults<ApPublicEntity> getAllConPublick(Pageable pageable, String title, String createUser, String publicType, String publicState) {

        QApPublicEntity qPub = QApPublicEntity.apPublicEntity;

        Predicate predicate = qPub.isNotNull().or(qPub.isNull());
        List<String> stationList=new ArrayList<String>();
        R r = serviceUtil.getUserInfoByRequest(request);
        if(r.getCode().equals("200")){
            LoginUser userInfo =  (LoginUser)r.get("data");
            stationList=userInfo.getRole();

            if(stationList!=null && !stationList.contains("地震部门审定人员")){
                predicate = ExpressionUtils.and(predicate, qPub.createUser.eq(userInfo.getLoginName()));
                predicate = ExpressionUtils.and(predicate, qPub.organName.eq(userInfo.getOrgName()));
            }
        }
        predicate = ("".equals(createUser) || createUser == null) ? predicate : ExpressionUtils.and(predicate, qPub.createUser.like("%"+createUser+"%"));
        predicate = ("".equals(title) || title == null) ? predicate : ExpressionUtils.and(predicate, qPub.title.like("%"+title+"%"));
        predicate = ("".equals(publicType) || publicType == null) ? predicate : ExpressionUtils.and(predicate, qPub.publicType.eq(publicType));
        predicate = ("".equals(publicState) || publicState == null) ? predicate : ExpressionUtils.and(predicate, qPub.publicState.eq(publicState));
        predicate = ExpressionUtils.and(predicate, qPub.delFlag.eq(Constant.NO_DELETE)) ;

        return  jpaQueryFactory
                .selectFrom(qPub)
                .where(
                        predicate
                )
                .orderBy(qPub.createTime.desc())
                .offset(pageable.getOffset())   //起始页
                .limit(pageable.getPageSize())  //每页大小
                .fetchResults();
    }

    /**
     * 添加拟制公告信息通过关联ID
     */
    @Override
    @Transactional
    public String addGlPublick(ApPublicEntity pub) {
        if(pub.getRelationId()==null){
            throw  new RuntimeException("拟制公告保存失败!");
        }
        R r = serviceUtil.getUserInfoByRequest(request);
        if(r.getCode().equals("200")){
            LoginUser userInfo =  (LoginUser)r.get("data");
            pub.setCreateUser(userInfo.getLoginName());
            pub.setOrganName(userInfo.getOrgName());
        }

        pub.setRevokeg(Constant.REVOKE_NO);
        pub.setDelFlag(Constant.NO_DELETE);
        pub.setCreateTime(new Date());
        pub.setInputenTime(new Date());
        pub.setPublicUrl("/dzjap/expertCred/getExpertCred");
        pub.setFictionType("1");
        try {
            publicDao.save(pub);
            //调取待办接口
            ApTodoEntity apTodoEntity = new ApTodoEntity();
            apTodoEntity.setStatus(Constant.STATUSDB);
            apTodoEntity.setCreateTime(new Date());
            apTodoEntity.setDelFlag(Constant.NO_DELETE);
            apTodoEntity.setTitle(pub.getTitle()+"公开公告审批");
            apTodoEntity.setLinkId(pub.getPublicId());
            //审批员才能看到相应的审批
            apTodoEntity.setStation("地震部门审定人员");
            //去审批页
            apTodoEntity.setUrl("../todolist/publick.html");
            apTodoEntity.setLocation(pub.getOrganName());
            apTodoEntity.setCreatePerson(ServiceUtil.getLoginName(request));
            //审批的业务
            apTodoEntity.setTypeFlag(Constant.TYPRFLAGSS);
            apTodoService.saveDB(apTodoEntity);
            return pub.getPublicId();

        }catch (Exception e){
            return "0";
        }
    }

    /**
     * 添加黑名单拟制公告信息通过关联ID
     */
    @Override
    @Transactional
    public String addGlRePublick(ApPublicEntity pub) {
        if(pub.getRelationId()==null){
            throw  new RuntimeException("拟制公告保存失败!");
        }
        R r = serviceUtil.getUserInfoByRequest(request);
        if(r.getCode().equals("200")){
            LoginUser userInfo =  (LoginUser)r.get("data");
            pub.setCreateUser(userInfo.getLoginName());
            pub.setOrganName(userInfo.getOrgName());
        }

        pub.setRevokeg(Constant.REVOKE_NO);
        pub.setDelFlag(Constant.NO_DELETE);
        pub.setCreateTime(new Date());
        pub.setInputenTime(new Date());
        pub.setPublicUrl("/dzjap/expertCred/getReExpertCred");
        pub.setFictionType("1");
        try {
            publicDao.save(pub);
            //调取待办接口
            ApTodoEntity apTodoEntity = new ApTodoEntity();
            apTodoEntity.setStatus(Constant.STATUSDB);
            apTodoEntity.setCreateTime(new Date());
            apTodoEntity.setDelFlag(Constant.NO_DELETE);
            apTodoEntity.setTitle(pub.getTitle()+"公开公告审批");
            apTodoEntity.setLinkId(pub.getPublicId());
            //审批员才能看到相应的审批
            apTodoEntity.setStation("地震部门审定人员");
            //去审批页
            apTodoEntity.setUrl("../todolist/publick.html");
            apTodoEntity.setLocation(pub.getOrganName());
            apTodoEntity.setCreatePerson(ServiceUtil.getLoginName(request));
            //审批的业务
            apTodoEntity.setTypeFlag(Constant.TYPRFLAGSS);
            apTodoService.saveDB(apTodoEntity);

            return pub.getPublicId();

        }catch (Exception e){
            return "0";
        }
    }



    /**
     * 通过主键Id,关联ID查询公开公告表
     */
    @Override
    public ApPublicEntity getGlPublick( String relationId) {
        if( StringUtils.isEmpty(relationId)){

            throw new RuntimeException("参数为空");
        }
        QApPublicEntity qPub = QApPublicEntity.apPublicEntity;
        return jpaQueryFactory
                .selectFrom(qPub)
                .where( qPub.relationId.eq(relationId),
                        qPub.delFlag.eq(Constant.NO_DELETE)
                )
                .fetchOne();
    }


    /**
     * 通过主键Id,关联ID查,类型询公开公告表
     */
    @Override
    public ApPublicEntity getGlTYPublick( String relationId,String enterType) {
        if( StringUtils.isEmpty(relationId)){

            throw new RuntimeException("参数为空");
        }
        QApPublicEntity qPub = QApPublicEntity.apPublicEntity;
        return jpaQueryFactory
                .selectFrom(qPub)
                .where( qPub.relationId.eq(relationId),
                        qPub.delFlag.eq(Constant.NO_DELETE),
                        qPub.enterType.eq(enterType)
                )
                .fetchOne();
    }



    /**
     * 添加拟制公告信息通过关联评价单位ID
     */
    @Override
    @Transactional
    public String addGlPjPublick(ApPublicEntity pub,String enterpriseCredittype) {
        if(pub.getRelationId()==null){
            throw  new RuntimeException("拟制公告保存失败!");
        }
        R r = serviceUtil.getUserInfoByRequest(request);
        if(r.getCode().equals("200")){
            LoginUser userInfo =  (LoginUser)r.get("data");
            pub.setCreateUser(userInfo.getLoginName());
            pub.setOrganName(userInfo.getOrgName());
        }

        pub.setRevokeg(Constant.REVOKE_NO);
        pub.setDelFlag(Constant.NO_DELETE);
        pub.setCreateTime(new Date());
        pub.setInputenTime(new Date());
        pub.setPublicUrl("/dzjap/enterPrise/getEnterPrise");
        pub.setFictionType("2");
        pub.setEnterType(enterpriseCredittype);
        try {
            publicDao.save(pub);

            //调取待办接口
            ApTodoEntity apTodoEntity = new ApTodoEntity();
            apTodoEntity.setStatus(Constant.STATUSDB);
            apTodoEntity.setCreateTime(new Date());
            apTodoEntity.setDelFlag(Constant.NO_DELETE);
            apTodoEntity.setTitle(pub.getTitle()+"公开公告审批");
            apTodoEntity.setLinkId(pub.getPublicId());
            //审批员才能看到相应的审批
            apTodoEntity.setStation("地震部门审定人员");
            //去审批页
            apTodoEntity.setUrl("../todolist/publick.html");
            apTodoEntity.setLocation(pub.getOrganName());
            apTodoEntity.setCreatePerson(ServiceUtil.getLoginName(request));
            //审批的业务
            apTodoEntity.setTypeFlag(Constant.TYPRFLAGSS);
            apTodoService.saveDB(apTodoEntity);
            return pub.getPublicId();

        }catch (Exception e){
            return "0";
        }
    }

    /**
     *  添加黑名单拟制公告信息通过关联评价单位Id
     */
    @Override
    @Transactional
    public String addGlPjRePublick(ApPublicEntity pub,String enterpriseCredittype) {
        if(pub.getRelationId()==null){
            throw  new RuntimeException("拟制公告保存失败!");
        }

        R r = serviceUtil.getUserInfoByRequest(request);
        if(r.getCode().equals("200")){
            LoginUser userInfo =  (LoginUser)r.get("data");
            pub.setCreateUser(userInfo.getLoginName());
            pub.setOrganName(userInfo.getOrgName());
        }

        pub.setRevokeg(Constant.REVOKE_NO);
        pub.setDelFlag(Constant.NO_DELETE);
        pub.setCreateTime(new Date());
        pub.setInputenTime(new Date());
        pub.setPublicUrl("/dzjap/enterPrise/getReEnterPrise");
        pub.setFictionType("2");
        pub.setEnterType(enterpriseCredittype);
        try {
            publicDao.save(pub);
            //调取待办接口
            ApTodoEntity apTodoEntity = new ApTodoEntity();
            apTodoEntity.setStatus(Constant.STATUSDB);
            apTodoEntity.setCreateTime(new Date());
            apTodoEntity.setDelFlag(Constant.NO_DELETE);
            apTodoEntity.setTitle(pub.getTitle()+"公开公告审批");
            apTodoEntity.setLinkId(pub.getPublicId());
            //审批员才能看到相应的审批
            apTodoEntity.setStation("地震部门审定人员");
            //去审批页
            apTodoEntity.setUrl("../todolist/publick.html");
            apTodoEntity.setLocation(pub.getOrganName());
            apTodoEntity.setCreatePerson(ServiceUtil.getLoginName(request));
            //审批的业务
            apTodoEntity.setTypeFlag(Constant.TYPRFLAGSS);
            apTodoService.saveDB(apTodoEntity);
            return pub.getPublicId();

        }catch (Exception e){
            return "0";
        }
    }

    /**
     *  添加拟制公告信息通过关联建设单位ID
     */
    @Override
    @Transactional
    public String addGlJsPublick(ApPublicEntity pub,String enterpriseCredittype) {
        if(pub.getRelationId()==null){
            throw  new RuntimeException("拟制公告保存失败!");
        }
        R r = serviceUtil.getUserInfoByRequest(request);
        if(r.getCode().equals("200")){
            LoginUser userInfo =  (LoginUser)r.get("data");
            pub.setCreateUser(userInfo.getLoginName());
            pub.setOrganName(userInfo.getOrgName());
        }

        pub.setRevokeg(Constant.REVOKE_NO);
        pub.setDelFlag(Constant.NO_DELETE);
        pub.setCreateTime(new Date());
        pub.setInputenTime(new Date());
        pub.setPublicUrl("/dzjap/enterJsPrise/getEnterPrise");
        pub.setFictionType("2");
        pub.setEnterType(enterpriseCredittype);
        try {
            publicDao.save(pub);

            //调取待办接口
            ApTodoEntity apTodoEntity = new ApTodoEntity();
            apTodoEntity.setStatus(Constant.STATUSDB);
            apTodoEntity.setCreateTime(new Date());
            apTodoEntity.setDelFlag(Constant.NO_DELETE);
            apTodoEntity.setTitle(pub.getTitle()+"公开公告审批");
            apTodoEntity.setLinkId(pub.getPublicId());
            //审批员才能看到相应的审批
            apTodoEntity.setStation("地震部门审定人员");
            //去审批页
            apTodoEntity.setUrl("../todolist/publick.html");
            apTodoEntity.setLocation(pub.getOrganName());
            apTodoEntity.setCreatePerson(ServiceUtil.getLoginName(request));
            //审批的业务
            apTodoEntity.setTypeFlag(Constant.TYPRFLAGSS);
            apTodoService.saveDB(apTodoEntity);
            return pub.getPublicId();

        }catch (Exception e){
            return "0";
        }
    }

    /**
     *  添加黑名单拟制公告信息通过关联建设单位Id
     */
    @Override
    @Transactional
    public String addGlJsRePublick(ApPublicEntity pub,String enterpriseCredittype) {
        if(pub.getRelationId()==null){
            throw  new RuntimeException("拟制公告保存失败!");
        }
        R r = serviceUtil.getUserInfoByRequest(request);
        if(r.getCode().equals("200")){
            LoginUser userInfo =  (LoginUser)r.get("data");
            pub.setCreateUser(userInfo.getLoginName());
            pub.setOrganName(userInfo.getOrgName());
        }

        pub.setRevokeg(Constant.REVOKE_NO);
        pub.setDelFlag(Constant.NO_DELETE);
        pub.setCreateTime(new Date());
        pub.setInputenTime(new Date());
        pub.setPublicUrl("/dzjap/enterJsPrise/getReEnterPrise");
        pub.setFictionType("2");
        pub.setEnterType(enterpriseCredittype);
        try {
            publicDao.save(pub);
            //调取待办接口
            ApTodoEntity apTodoEntity = new ApTodoEntity();
            apTodoEntity.setStatus(Constant.STATUSDB);
            apTodoEntity.setCreateTime(new Date());
            apTodoEntity.setDelFlag(Constant.NO_DELETE);
            apTodoEntity.setTitle(pub.getTitle()+"公开公告审批");
            apTodoEntity.setLinkId(pub.getPublicId());
            //审批员才能看到相应的审批
            apTodoEntity.setStation("地震部门审定人员");
            //去审批页
            apTodoEntity.setUrl("../todolist/publick.html");
            apTodoEntity.setLocation(pub.getOrganName());
            apTodoEntity.setCreatePerson(ServiceUtil.getLoginName(request));
            //审批的业务
            apTodoEntity.setTypeFlag(Constant.TYPRFLAGSS);
            apTodoService.saveDB(apTodoEntity);
            return pub.getPublicId();

        }catch (Exception e){
            return "0";
        }
    }

    /**
     * 带有标题参数查询list
     * @author wt
     * @date 2020\3\10 0010
     */
    @Override
    public List titleList(String title,String publicType,String isPublic) {

        QApPublicEntity qPub = QApPublicEntity.apPublicEntity;
        Predicate predicate = qPub.isNotNull().or(qPub.isNull());
        predicate = ("".equals(title) || title == null) ? predicate : ExpressionUtils.and(predicate, qPub.title.like("%"+title+"%"));
        predicate = ExpressionUtils.and(predicate, qPub.publicState.eq("1")) ;
        if(publicType.equals("0")){
            predicate = ExpressionUtils.and(predicate, qPub.publicType.ne("1"));
        }else{
            predicate = ExpressionUtils.and(predicate, qPub.publicType.ne("0"));
        }
        predicate = ExpressionUtils.and(predicate, qPub.publishType.eq("1")) ;
        predicate = ExpressionUtils.and(predicate, qPub.revokeg.eq("0")) ;
        predicate = ExpressionUtils.and(predicate, qPub.delFlag.eq(Constant.NO_DELETE)) ;
        predicate = ExpressionUtils.and(predicate, qPub.isPublic.eq(isPublic)) ;

        return jpaQueryFactory
                .selectFrom(qPub)
                .where(
                        predicate
                )
                .orderBy(qPub.publishTime.desc())
                .fetch();

    }

    /**
     * 不带参数查询list
     * @author wt
     * @date 2020\3\10 0010
     */
    @Override
    public List allList(String publicType,String isPublic) {
        QApPublicEntity qPub = QApPublicEntity.apPublicEntity;
        Predicate predicate = qPub.isNotNull().or(qPub.isNull());
        predicate = ExpressionUtils.and(predicate, qPub.publicState.eq("1")) ;
        predicate = ExpressionUtils.and(predicate, qPub.publishType.eq("1")) ;
        predicate = ExpressionUtils.and(predicate, qPub.revokeg.eq("0")) ;
        if(publicType.equals("0")){
            predicate = ExpressionUtils.and(predicate, qPub.publicType.ne("1"));
        }else{
            predicate = ExpressionUtils.and(predicate, qPub.publicType.ne("0"));
        }
        predicate = ExpressionUtils.and(predicate, qPub.delFlag.eq(Constant.NO_DELETE)) ;
        predicate = ExpressionUtils.and(predicate, qPub.isPublic.eq(isPublic)) ;

        return  jpaQueryFactory
                .selectFrom(qPub)
                .where(
                        predicate
                )
                .orderBy(qPub.publishTime.desc())
                .fetch();
    }

    /**
     *通过id查询详情
     * @author wt
     * @date 2020\3\12 0012
     */
    @Override
    public ApPublicEntity getEntity(String id) {

        if(StringUtils.isEmpty(id) ){

            throw new RuntimeException("参数为空");
        }
        QApPublicEntity qPub = QApPublicEntity.apPublicEntity;
        return jpaQueryFactory
                .selectFrom(qPub)
                .where( qPub.publicId.eq(id),
                        qPub.publicState.eq("1"),
                        qPub.publishType.eq("1"),
                        qPub.revokeg.eq("0"),
                        qPub.delFlag.eq(Constant.NO_DELETE)
                )
                .fetchOne();
    }

}
